/*******************************************************************************
* @file led.c
* @author hai
* @version V1.0
* @date 2017-10-25
* @brief
********************************************************************************/

#include "Sys.h"

typedef struct{
    GPIO_TypeDef *gpiox;
    INT16U            pinx;
    INT8U              on;
    INT8U              off;
}LED_ST;


const LED_ST ledtbl[LED_NUM] = {
    {GPIOB,     GPIO_Pin_11,     0,  1}, 
    {GPIOB,     GPIO_Pin_10,     0,  1}, 
		
		{GPIOB,     GPIO_Pin_1,     0,  1}, 
		{GPIOB,     GPIO_Pin_2,     0,  1},  
		
		{GPIOA,     GPIO_Pin_7,     0,  1}, 
    {GPIOB,     GPIO_Pin_0,     0,  1}, 
		
		{GPIOA,     GPIO_Pin_3,     0,  1}, 
		{GPIOA,     GPIO_Pin_4,     0,  1},  
};

static INT16U leddelay[LED_NUM];


void led_on(led_type_e type,  uint16_t delayunit)
{
    if (leddelay[type] ==0)
		{ 
        leddelay[type] = delayunit; 
        PIO_Write(ledtbl[type].gpiox, ledtbl[type].pinx, ledtbl[type].on); 
    }    
}

void led_scan (void)
{
    INT8U i;

    for (i = 0; i<LED_NUM; i++)
	  {
        if (leddelay[i] > 0)
				{
            if ((--leddelay[i]) == 0)
						{                
                PIO_Write(ledtbl[i].gpiox, ledtbl[i].pinx, ledtbl[i].off);
            }
        }
    }
}

static void led_delay(uint32_t ms)
{
	uint32_t cur_tick = 0;
	cur_tick = GetTick();
	
	while(cur_tick - GetTick() <= ms);
}

void led_init(void)
{
    INT8U i;

    for (i=0; i<LED_NUM; i++)
	  {
        PIO_Config(ledtbl[i].gpiox,  ledtbl[i].pinx,  GPIO_Mode_Out_PP, GPIO_Speed_2MHz);
        PIO_Write(ledtbl[i].gpiox,  ledtbl[i].pinx,  ledtbl[i].off);    
    }
    
    mem_set(leddelay, 0, sizeof(leddelay));    
		
		for (i=0; i<4; i++)
	  {
			led_on(0+i*2,10);
		}
		led_delay(200);
		for (i=0; i<4; i++)
	  {
			led_on(1+i*2,100);
		}
}
